﻿using PSI.Models.TMS;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;

namespace PSI.Helper
{
    public class MakePDF
    {
        public void CreatImprotPDF(InConsignationDetail model)
        {
            Font font8 = BaseFontAndSize(2, 14,"",false);
            Font font7 = BaseFontAndSize(2, 8, "", false);
            Font BugFont = BaseFontAndSize(2, 25, "",true);
            Font hearfont = BaseFontAndSize(2,25,"",false);
            Document document = new Document(PageSize.A3,30,30,20,20);
   
            MemoryStream MStream = new MemoryStream();
            PdfWriter writer=PdfWriter.GetInstance(document, MStream);
            document.Open();

            #region 头部信息
            string imagepath = HttpContext.Current.Server.MapPath("/Images/QQ.jpg");
            iTextSharp.text.Image gif = iTextSharp.text.Image.GetInstance(imagepath);
            gif.ScalePercent(45f);

            PdfPTable heardtbl = new PdfPTable(2);
            heardtbl.LockedWidth = true;
            heardtbl.TotalWidth = document.PageSize.Width-100f;
            heardtbl.SetWidths(new float[] { 1f, 1f });
            PdfPCell rowcell = new PdfPCell(gif);
            rowcell.BorderWidth = 0;
            heardtbl.AddCell(rowcell);
            Paragraph hh = new Paragraph(10f,"吉林省陆港物流有限公司\n", hearfont);
            hh.Add(new Phrase("JILIN PROVINCE LUGANG LOGISTICS CO.,LTD\n", font8));
            hh.Add(new Phrase("电话:0417-6151022  传真: 0417-6151098\n", font8));
            hh.Add(new Phrase("地址:辽宁省营口市鲅鱼圈区辽东湾大街北段\n", font8));
            rowcell = new PdfPCell(hh);
            rowcell.HorizontalAlignment = Element.ALIGN_RIGHT;
            rowcell.BorderWidth = 0;
            heardtbl.AddCell(rowcell);
            document.Add(heardtbl);
            #endregion

            #region 表格

            Paragraph duanluo = new Paragraph("客  户  签  收  单", BugFont);
            duanluo.Alignment = Element.ALIGN_CENTER; //段落对齐方式
           // duanluo.Leading = 15; //段落间距 
            document.Add(duanluo);
         
                
            PdfPTable tbl = new PdfPTable(4);
            tbl.SpacingBefore = 10f;
            tbl.TotalWidth = document.PageSize.Width-100f;
            tbl.LockedWidth = true;  //这句很有用
            float[] width=new float[]{1.5f,3.5f,1.5f,3.5f};
           
            tbl.SetWidths(width);
             
            PdfPCell pcellID = new PdfPCell(new Phrase(new Chunk("船名/航次", font8)));

            pcellID.Padding = 5f;
            pcellID.HorizontalAlignment = Element.ALIGN_CENTER;
            pcellID.VerticalAlignment =Element.ALIGN_MIDDLE ;
            PdfPCell pcellID1 = new PdfPCell(new Phrase(new Chunk(model.inconsig.Vsl + "/" + model.inconsig.VoyNumber, font8)));

            PdfPCell pcellID2 = new PdfPCell(new Phrase(new Chunk("运单号", font8)));
            pcellID2.VerticalAlignment = Element.ALIGN_MIDDLE;
            pcellID2.HorizontalAlignment = Element.ALIGN_CENTER;
            PdfPCell pcellID3 = new PdfPCell(new Phrase(new Chunk(model.BlNumber, font8)));
            tbl.AddCell(pcellID);
            tbl.AddCell(pcellID1);
            tbl.AddCell(pcellID2);
            tbl.AddCell(pcellID3);
            PdfPCell row2cell1 = new PdfPCell(new Phrase(new Chunk("箱号/封号", font8)));
            row2cell1.Rowspan = 2;
           // row2cell1.Padding = 10f;
            row2cell1.HorizontalAlignment = 1;
            row2cell1.VerticalAlignment = Element.ALIGN_MIDDLE;

            tbl.AddCell(row2cell1);
            row2cell1 = new PdfPCell(new Phrase(new Chunk(model.CntNumber1, font8)));
            row2cell1.Colspan = 3;
            tbl.AddCell(row2cell1);
            row2cell1 = new PdfPCell(new Phrase(new Chunk(model.CntNumber2, font8)));
            row2cell1.Colspan = 3;
            tbl.AddCell(row2cell1);

            PdfPCell row3 = new PdfPCell(new Phrase(new Chunk("箱型/箱量", font8)));
            row3.Padding = 5f;
            row3.HorizontalAlignment = 1;
            tbl.AddCell(row3);
            row3 = new PdfPCell(new Phrase(new Chunk(model.CntType, font8)));
            tbl.AddCell(row3);
            row3 = new PdfPCell(new Phrase(new Chunk("箱  属", font8)));
            row3.HorizontalAlignment = 1;
            tbl.AddCell(row3);
            row3 = new PdfPCell(new Phrase(new Chunk(model.CntOwner, font8)));
            tbl.AddCell(row3);
            PdfPCell row4 = new PdfPCell(new Phrase(new Chunk("货    名", font8)));
            row4.Padding = 5f;
            row4.HorizontalAlignment = 1;
            tbl.AddCell(row4);
            row4 = new PdfPCell(new Phrase(new Chunk(model.Goods, font8)));
            row4.Colspan = 3;
            tbl.AddCell(row4);
            PdfPCell row5 = new PdfPCell(new Phrase(new Chunk("收 货 人", font8)));
            row5.Padding = 5f;
            row5.HorizontalAlignment = 1;
            tbl.AddCell(row5);
            row5 = new PdfPCell(new Phrase(new Chunk(model.GoodsOwner, font8)));
            tbl.AddCell(row5);
            row5 = new PdfPCell(new Phrase(new Chunk("联系人电话", font8)));
            row5.HorizontalAlignment = 1;
            tbl.AddCell(row5);
            row5 = new PdfPCell(new Phrase(new Chunk(model.GoodsOwnerTel, font8)));
            tbl.AddCell(row5);

            PdfPCell row6 = new PdfPCell(new Phrase(new Chunk("收货地址", font8)));
            row6.Padding = 5f;
            row6.HorizontalAlignment = 1;
            tbl.AddCell(row6);
            row6 = new PdfPCell(new Phrase(new Chunk(model.Address, font8)));
            row6.Colspan = 3;
            tbl.AddCell(row6);

            PdfPCell row7 = new PdfPCell(new Phrase(new Chunk("代 收 款", font8)));
            row7.Padding = 5f;
            row7.HorizontalAlignment = 1;
            tbl.AddCell(row7);
            row7 = new PdfPCell(new Phrase(new Chunk("5600.00", font8)));
            row7.Colspan = 3;
            tbl.AddCell(row7);
            PdfPCell row8 = new PdfPCell(new Phrase(new Chunk("备    注", font8)));
            row8.Padding = 5f;
            row8.HorizontalAlignment = 1;
            tbl.AddCell(row8);
            row8 = new PdfPCell(new Phrase(new Chunk(model.Memo, font8)));
            row8.Colspan = 3;
            tbl.AddCell(row8);
        
            document.Add(tbl); 
            #endregion

            #region 表格下字体
            Font font2=BaseFontAndSize(1,15,"",false);
            Font font1 = BaseFontAndSize(1, 20, "", false);
            Paragraph chunk1 = new Paragraph("司机注意:", font1);
            chunk1.FirstLineIndent = 15;
            document.Add(chunk1);
            List listt = new List(List.ORDERED, 20f);
            listt.IndentationLeft = 35f;
            listt.Add(new iTextSharp.text.ListItem("在码头公司提箱时一定检验箱体、箱封完好,方可提箱。",font2));
            listt.Add(new iTextSharp.text.ListItem("送达货主后,请货主检查该票货物箱体、箱封是否无损,并请货主在此签收单上签字（盖章）。如有代收款,必须先收款后开箱卸货。", font2));
            listt.Add(new iTextSharp.text.ListItem("将签收单和代收款带回我司。",font2));
            document.Add(listt);
            chunk1 = new Paragraph("若司机违反上述规定,出现任何问题由送货司机负责。",font2);
            chunk1.FirstLineIndent = 55f;
            document.Add(chunk1);
            chunk1 = new Paragraph(new Chunk("车号:                                ",font2));
            Chunk chun = new Chunk("司机签字:_______________",font2);
            chunk1.FirstLineIndent = 55f;
            chunk1.Add(chun);
            document.Add(chunk1);

            chunk1 = new Paragraph("货主注意:",font1);
            chunk1.SpacingBefore = 40f;
            chunk1.FirstLineIndent = 14f;
            document.Add(chunk1);
            listt = new List(List.ORDERED, 20f);
            listt.IndentationLeft = 35f;
            listt.Add(new iTextSharp.text.ListItem("开箱前请检验铅封是否完好,如有问题请在签收单上注明,否则视为完好。如有代收款请先付款后卸货。", font2));
            listt.Add(new iTextSharp.text.ListItem("卸货时如发现货物异常现象,请及时与我司联系。", font2));
            listt.Add(new iTextSharp.text.ListItem("根据港口船务公司规定,保证卸货时间:20尺2小时,40尺3小时。如果卸货超时将加收超时费,超时一小时加收100元。", font2));
            document.Add(listt);

            chunk1 = new Paragraph(new Chunk("收货人签字（盖章）:",font2));
            chunk1.FirstLineIndent = 40f;
            document.Add(chunk1);
            #endregion

            document.Close();
            HttpContext.Current.Response.ContentType = "application/pdf";
            HttpContext.Current. Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode("创建PDF.pdf", System.Text.Encoding.UTF8));
            HttpContext.Current. Response.BinaryWrite(MStream.GetBuffer());
            HttpContext.Current. Response.Flush();
            HttpContext.Current. Response.End();
        
        }


        /// <summary>
        /// 设置字体
        /// </summary>
        /// <param name="type">0是黑体1是楷体2是宋体</param>
        /// <param name="size">大小</param>
        /// <param name="color">颜色</param>
        /// <returns></returns>
        private Font BaseFontAndSize(int type, int size, string color, bool bold)//设置文字的大小类型和颜色  
        {
            BaseFont bf;
            Font font = null;
            var fontstyle = bold ? Font.BOLD : Font.NORMAL;
            // BaseColor c =new BaseColor(int.Parse(color, System.Globalization.NumberStyles.AllowHexSpecifier));
            switch (type)
            {
                case 0:
                    bf = BaseFont.CreateFont("C:\\WINDOWS\\FONTS\\SIMHEI.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);//字体：黑体   
                    font = new Font(bf, size, fontstyle, BaseColor.BLACK);
                    break;
                case 1:
                    bf = BaseFont.CreateFont("C:\\WINDOWS\\FONTS\\simkai.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);//字体：楷体  
                    font = new Font(bf, size, fontstyle, BaseColor.BLACK);
                    break;
                case 2:
                    bf = BaseFont.CreateFont("C:\\WINDOWS\\FONTS\\simfang.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);//字体：宋体  
                    font = new Font(bf, size, fontstyle, BaseColor.BLACK);
                    break;
            }
            return font;
        }
    }
}